package com.example.exam.repository;

import com.example.exam.entity.ExamPaperAnswer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author shishuqian
 * date 2021/4/27
 * time 15:15
 **/
@Repository
public interface ExamPaperAnswerRepository extends JpaRepository<ExamPaperAnswer,Integer> {

    //根据用户id和试卷id查找答题卷(正常情况下只会有一条数据，但在测试时可能重复添加数据，为防止报错，返回List集合)
    @Query(value = "SELECT p FROM ExamPaperAnswer p WHERE p.userId = ?1 AND p.examPaperId = ?2")
    List<ExamPaperAnswer> findByUserIdAndExamPaperId(Integer userId, Integer examPaperId);

    //查询用户考过的考试
    @Query(value = "SELECT p FROM ExamPaperAnswer p WHERE p.userId = ?1")
    List<ExamPaperAnswer> findExamPaperAnswerByUserId(Integer userId);

    //考试记录，分页查询，根据创建时间降序查找
    Page<ExamPaperAnswer> findAllByUserIdOrderByCreateTimeDesc(Integer userId, Pageable pageable);

    //查询待批改/已批改的试卷,根据创建时间升序查找,未完成提交的不批改
    @Query(value = "SELECT p FROM ExamPaperAnswer p WHERE p.examPaperId = ?1 AND p.status = ?2 AND p.finishStatus = true ORDER BY p.createTime ASC ")
    Page<ExamPaperAnswer> getCheck(Integer examPaperId,Integer status,Pageable pageable);

    //根据已做试卷名搜索
    Page<ExamPaperAnswer> findByPaperNameLikeAndUserIdOrderByCreateTimeDesc(String paperName,Integer userId,Pageable pageable);

    //根据试卷id查询此试卷的填写情况
    @Query(value = "select real_name from t_exam_paper_answer where exam_paper_id = ?",nativeQuery = true)
    List<String> findAllByExamPaperId(Integer examPaperId);
}
